$(function() {
	reloadVerifyCode('getImageCode');
	if($("#type").val() == '0'){
		var showImageCode = $("#showImageCode").val();
		if(showImageCode == 'true'){
			$("#divImageCode").show();
		} else {
			$("#divImageCode").hide();
		}
	} else {
		$("#divImageCode").hide();
	}
	$("#choose").change(function() {
		gotoHtml('/setting/verify.htm?type=' + $("#type").val() + "&verify=" + $(this).val());
	});

	var InterValObj;// timer变量，控制时间
	var curCount = 60;// 当前剩余秒数
	$("#getCodeByMobile").bind("click", function() {
		showLoading();
		$.ajax({
			type : "get",
			url : appPath + "/member/sendCodeByMobile.html",
			data : {
				tid : new Date().getTime(),
				loginname : $("#loginname").val()
			},
			success : function(result) {
				hideLoading();
				myalert(result.msg);
				if (result.code == '1') {
					curCount = 60;
					InterValObj = window.setInterval(setRemainTime, 1000);
				} else if (result.code == '-2') {
					curCount = result.second;
					InterValObj = window.setInterval(setRemainTime, 1000);
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
			}
		});
	});
	// 验证码有效期倒计时
	function setRemainTime() {
		if (curCount == 0) {
			window.clearInterval(InterValObj);// 停止计时器
		} else {
			curCount--;
		}
	}

	$("#getCodeByEmail").bind("click", function() {
		showLoading();
		$.ajax({
			type : "get",
			url : appPath + "/member/sendCode.html",
			data : {
				tid : new Date().getTime(),
				loginname : $("#loginname").val()
			},
			success : function(result) {
				hideLoading();
				myalert(result.msg);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
			}
		});
	});

	var InterValObj1;// timer变量，控制时间
	var curCount1 = 60;// 当前剩余秒数
	$("#getCodeByNewMobile").bind("click", function() {
		var oldmobile = $("#loginname").val();
		var newmobile = $("#account").val();
		var mobileResult = checkMobile(newmobile);
		if (!mobileResult.result) {
			myalert(mobileResult.msg);
			return false;
		}
		if (oldmobile == newmobile) {
			myalert("新手机号与原手机号相同");
			return false;
		}
		var imagecode = "";
		if($("#imagecode").length > 0){
			if(!$("#divImageCode").is(':hidden')){
				imagecode = $("#imagecode").val();
				if(imagecode == '' || imagecode == '请输入验证码'){
					myalert('请输入图形验证码');
					return false;
				}
			}
		}
		showLoading();
		$.ajax({
			type : "get",
			url : appPath + "/member/sendCode.html",
			data : {
				tid : new Date().getTime(),
				loginname : newmobile,
				imagecode : imagecode
			},
			success : function(result) {
				hideLoading();
				myalert(result.msg);
				if (result.code == '1') {
					curCount1 = 60;
					InterValObj1 = window.setInterval(setRemainTime1, 1000);
				} else if (result.code == '-2') {
					curCount1 = result.second;
					InterValObj1 = window.setInterval(setRemainTime1, 1000);
				}
				if($("#type").val() == '0'){
					if(result.showImageCode == '1'){
						$("#divImageCode").show();
					} else {
						$("#divImageCode").hide();
					}
				}
				if(result.msg.indexOf("图形验证码错误")>-1){
					reloadVerifyCode('getImageCode');
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
			}
		});
	});
	// 验证码有效期倒计时
	function setRemainTime1() {
		if (curCount1 == 0) {
			window.clearInterval(InterValObj1);// 停止计时器
		} else {
			curCount1--;
		}
	}

	$("#getCodeByNewEmail").bind("click", function() {
		// 此按钮显示表示修改邮箱，手机号跟邮箱本身在格式上不同，所以不需要判断type与verify，直接对比老帐号与新账号是否相同即可
		var oldemail = $("#loginname").val();
		var newemail = $("#account").val();
		if ('' == newemail || newemail == '请输入新邮箱地址') {
			myalert("请输入新邮箱地址");
			return false;
		}
		var emailResult = checkEmail(newemail);
		if (!emailResult.result) {
			myalert(emailResult.msg);
			return false;
		}
		if (oldemail == newemail) {
			myalert("新邮箱地址与老邮箱地址相同");
			return false;
		}
		showLoading();
		$.ajax({
			type : "get",
			url : appPath + "/member/sendCode.html",
			data : {
				tid : new Date().getTime(),
				loginname : newemail
			},
			success : function(result) {
				hideLoading();
				myalert(result.msg);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
			}
		});
	});
})

function edit() {
	var code = $("#code").val();
	if (code == '' || code == '请输入验证码') {
		myalert("验证码不能为空");
		return false;
	}
	var type = $("#type").val();
	var account = $("#account").val();
	var newcode = $("#newcode").val();
	if (type == 0) {
		if(account == '请输入新手机号' || account == ''){
			myalert("新手机号不能为空");
			return false;
		}
		var mobileResult = checkMobile(account);
		if (!mobileResult.result) {
			myalert(mobileResult.msg);
			return false;
		}
		if (newcode == '' || newcode == '请输入验证码') {
			myalert("新手机号验证码不能为空");
			return false;
		}
	} else if (type == 1) {
		if(account == '请输入新邮箱地址' || account == ''){
			myalert("新邮箱地址不能为空");
			return false;
		}
		var emailResult = checkEmail(account);
		if (!emailResult.result) {
			myalert(emailResult.msg);
			return false;
		}
		if (newcode == '' || newcode == '请输入验证码') {
			myalert("新邮箱验证码不能为空");
			return false;
		}
	}
	showLoading();
	$.ajax({
		type : "get",
		url : appPath + "/setting/editmobileoremail.html",
		data : {
			loginname : $("#loginname").val(),
			code : $("#code").val(),
			account : $("#account").val(),
			type : $("#type").val(),
			verify : $("#verify").val(),
			newcode : $("#newcode").val(),
			token : $.cookie("token")
		},
		success : function(result) {
			hideLoading();
			myalert(result.msg, function() {
				if (result.code == '1') {
					gotoHtml('/center/center.htm');
				} else if (result.code == '-1') {
					window.location = appPath + "/member/login.htm";
				}
			});
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			hideLoading();
			myalert("系统异常，请稍后再试");
		}
	});
}